package com.allwees.bs.module.oms.service.impl;

import com.allwees.bs.core.model.order.wf.order.EOrderAction;
import com.allwees.bs.core.model.order.wf.refund.ERefundAction;
import com.allwees.bs.core.model.order.wf.refund.ERefundState;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.oms.dao.OmsOrderDao;
import com.allwees.bs.module.oms.dao.OmsOrderItemDao;
import com.allwees.bs.module.oms.dao.OrderDao;
import com.allwees.bs.module.oms.dao.OrderItemDao;
import com.allwees.bs.module.oms.dao.OrderRefundDao;
import com.allwees.bs.module.oms.dao.OrderRefundItemDao;
import com.allwees.bs.module.oms.entity.OmsOrder;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.entity.Order;
import com.allwees.bs.module.oms.entity.OrderItem;
import com.allwees.bs.module.oms.entity.OrderRefund;
import com.allwees.bs.module.oms.entity.OrderRefundItem;
import com.allwees.bs.module.oms.req.RetailerCancelShippedReq;
import com.allwees.bs.module.oms.service.BaseOmsOrderService;
import com.allwees.bs.module.oms.service.GeneralOmsOrderItemService;
import com.allwees.bs.module.oms.service.GeneralOmsOrderService;
import com.allwees.bs.module.oms.wf.EOmsOrderAction;
import com.allwees.core.common.constant.EStatus;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * oms order service
 *
 * @author mouhn
 * @since 2020-1-3 10:01
 */
public abstract class BaseOmsOrderServiceImpl extends BoServiceImpl<OmsOrderDao, OmsOrder> implements BaseOmsOrderService {

    @Autowired
    protected OrderDao orderDao;

    @Autowired
    protected OrderItemDao orderItemDao;
    @Autowired
    protected GeneralOmsOrderItemService generalOmsOrderItemService;

    @Autowired
    protected GeneralOmsOrderService generalOmsOrderService;

    @Autowired
    protected  OmsOrderItemDao omsOrderItemDao;

    @Autowired
    protected OrderRefundDao orderRefundDao;

    @Autowired
    protected OrderRefundItemDao orderRefundItemDao;

    protected void _signal(String orderUuid, EOmsOrderAction action) {
        OmsOrder omsOrder = checkByUuid(orderUuid);
        omsOrder.signal(action);
        updateByUuid(omsOrder);
    }

    protected void _doShipped(String orderUuid) {
        EOmsOrderAction omsOrderAction = EOmsOrderAction.MERCHANT_SHIPPED;
        EOrderAction orderAction = EOrderAction.MERCHANT_SHIPPED;

        OmsOrder omsOrder = checkByUuid(orderUuid);

        int orderRefundCount = orderRefundDao.getCountByOrderUuidAndState(orderUuid, ERefundState.COMPLETED);

        if (omsOrder.getShippedCount() >= (omsOrder.getQuantity() - orderRefundCount)) {
            omsOrderAction = EOmsOrderAction.MERCHANT_ALL_SHIPPED;
            orderAction = EOrderAction.MERCHANT_ALL_SHIPPED;
        }

        //更新
        omsOrder.signal(omsOrderAction);
        updateByUuid(omsOrder);

        // order 同步
        Order order = orderDao.findByUuid(orderUuid);
        order.signal(orderAction);
        orderDao.updateByUuid(order, orderUuid);
    }

    protected void _doCancelShipped(RetailerCancelShippedReq req, OrderItem orderItem, Order order, OmsOrderItem omsOrderItem) {

        OrderRefund orderRefund =  new OrderRefund(req,order,orderItem);
        orderRefund.setState(ERefundState.START);

        ERefundAction refundAction = ERefundAction.RETAILER_CANCEL_SHIPPED_ON_LIMIT;
        if(orderItem.getCanShipping()){
            refundAction = ERefundAction.RETAILER_CANCEL_SHIPPED_AFTER_LIMIT;
        }
        orderRefund.signal(refundAction);
        orderRefund.setEStatus(EStatus.OMS_REFUND);


        //orderRefund.setOrderShippingState(req.getShippingState());
        orderRefundDao.insert(orderRefund);
        OrderRefundItem orderRefundItem = new OrderRefundItem(orderItem);
        orderRefundItem.setOrderRefundUuid(orderRefund.getUuid());
        orderRefundItemDao.insert(orderRefundItem);

        generalOmsOrderItemService.doRefundedByRetailer(orderRefund.getOrderItemUuid());
        generalOmsOrderService.doRefundedByRetailer(orderRefund.getOrderUuid(),orderRefund.getOrderItemUuid());

    }
}
